package com.situ.fly.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.situ.fly.entity.Answer;

@Mapper
public interface AnswerMapper {

	/**
	 * 插入数据库
	 */
	@Insert("insert into answer (content, qid, uid) value(#{content}, #{qid}, #{uid})")
	int insert(Answer answer);

	/**
	 * 
	 * @param qid
	 * @return
	 */
	@Select("<script>select answer.*, " + 
			"	`user`.head, " + 
			"	`user`.nickname, " + 
			"	`user`.vip, " + 
			"	user.role, " + 
			"	user.cert, " + 
			"	user.status ustatus " + 
			"	<if test='uid!=-1'>, l.id as like_id </if> " + 
			"from answer " + 
			"	left join `user` on answer.uid = `user`.id " + 
			"	<if test='uid!=-1'> " +
			"left join " +
			"(select * from `like` where `like`.uid=#{uid} ) as l on answer.id = l.aid" +
			"</if> " + 
			"where qid=#{qid} "
			+ "order by answer.createtime" +
			"</script>")
	List<Map<String, Object>> selectByQid(Integer qid, Integer uid);

	/**
	 * 根据用户查询回复
	 * @param uid
	 * @return
	 */
	@Select("select answer.*, question.title from answer, question "
			+ "where answer.qid=question.id and answer.uid=#{uid} order by createtime desc")
	List<Map<String, Object>> selectByUid(Integer uid);

	/**
	 * 根据 ID查询回复
	 * @param id
	 * @return
	 */
	@Select("select * from answer where id=#{id}")
	Answer selectById(Integer id);

	/**
	 * 修改帖子
	 * @param answer
	 * @return
	 */
	@Update("<script>update answer "
			+ "<set> "
			+ "<if test='content!=null'>content=#{content}, </if> "
			+ "<if test='status!=null'>status=#{status}, </if> "
			+ "</set> "
			+ "where id=#{id}"
			+ "</script>")
	int update(Answer answer);
	
}
